package com.ssm.controller.student;

import com.alibaba.fastjson.JSONObject;
import com.ssm.controller.BaseController;
import com.ssm.entity.*;
import com.ssm.service.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Random;

@Controller
public class StudentAppliesController extends BaseController {
    @Resource(name = "appliesService")
    private AppliesService appliesService;
    @Resource(name = "studentService")
    private StudentService studentService;
    @Resource(name = "noticeList")
    private NoticeListService noticeListService;
    @Resource(name = "Departmentname")
    private DepartmentService departmentService;
    @Resource(name = "departmentPeopleService")
    private DepartmentPeopleService departmentPeopleService;
    // 查询
    @RequestMapping(value ="applies/manage",method = RequestMethod.GET)
    public String AppliesMageageQuery(HttpSession session, Map<String,Object> map){
        List<RecruitApplies> recruitApplies=new ArrayList<>();
        logger.info("检查管理员权限");
        String adminId=checkAdmin(session);
        if (!adminId.equals("adminId")){
            return "redirect:/";
        }
        recruitApplies=appliesService.listAllRecruitApplies();
        //logger.info("--------======>"+recruitApplies.get(0));
        List<UmStudent> studentList=new ArrayList<>();
        RecruitApplies recruitApplies1=new RecruitApplies();
        List<Departmentname> dd=new ArrayList<>();

        for (int j=0;j<recruitApplies.size();j++){
            //获取学生信息
            recruitApplies1= recruitApplies.get(j);
            UmStudent student= studentService.getUmStudent(recruitApplies1.getId());
            studentList.add(student);
            //获取部门信息
            RecruitInfo recruitInfo= noticeListService.getNoticeList(recruitApplies1.getInfoid());
            String s= recruitInfo.getDeparmentid();

            Departmentname departmentname= departmentService.nametoDepartment(s);
            dd.add(departmentname);
        }



        //组装
        List<Applieslist> list=new ArrayList<>();

        for (int len=0;len<recruitApplies.size();len++){
            Applieslist applieslist=new Applieslist();
            UmStudent ss= studentList.get(len);
            applieslist.setId(ss.getStudentid());
            //logger.info("-------mingzi--------------->"+ss.getName());
            applieslist.setName(ss.getName());
            applieslist.setEmail(ss.getEmail());
            applieslist.setZhuanye(ss.getMajor());
            applieslist.setDizhi(ss.getPosition());
            applieslist.setPhonenumber(ss.getPhonenumber().toString());
            applieslist.setBumen(dd.get(len).getdepartmentname()) ;
            applieslist.setLiyou(recruitApplies.get(len).getIntroduce());
            applieslist.setAppid(recruitApplies.get(len).getAppliesid());
            list.add(applieslist);

            logger.info("-===================>"+recruitApplies.get(len).getIntroduce());
        }
        map.put("list",list);
        for (int c = 0; c < list.size(); c++) {
            logger.info(list.get(c).toString());
        }
        return "admin/appliesManage";
    }
    //添加
    @ResponseBody
    @RequestMapping(value = "applies/add",method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    public String add(HttpSession session, @RequestParam Integer id ,@RequestParam String introduce){
        logger.info("检查管理员权限");
        String adminId=checkAdmin(session);
        if (!adminId.equals("studentId")){
            return "redirect:/";
        }
        JSONObject object=new JSONObject();
        Object o=session.getAttribute("studentId");
        RecruitApplies recruitApplies=new RecruitApplies();
        recruitApplies.setId(Integer.parseInt(o.toString()));
        //recruitApplies.setAppliesid(id);
        recruitApplies.setInfoid(id);
        recruitApplies.setIntroduce(introduce);
        Integer i= appliesService.IsgetstudentidtoRecruitApplies(Integer.parseInt(o.toString()));
        if(i==1){
            object.put("status","have");
        }else {
            Integer IsSuccuess = appliesService.createRecruitApplies(recruitApplies);
            if (IsSuccuess == 1) {
                logger.info("添加成功");

                object.put("status", "success");
            } else {
                logger.info("添加错误");
                object.put("status", "false");
            }
        }
        return object.toJSONString();
    }
    //删除
    @ResponseBody
    @RequestMapping(value = "applies/status",method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    public String delete(HttpSession session,  @RequestParam Integer id,@RequestParam Integer status){
        logger.info("检查管理员权限");
        String adminId=checkAdmin(session);
        if (!adminId.equals("adminId")){
            return "redirect:/";
        }
        Integer IsSuccess=0;
       // Integer IsSuccess=appliesService.deleteRecruitApplies(id);
        if (status==0){
             IsSuccess= appliesService.deleteRecruitApplies(id);
             //待后面添加 通知学生代码
        }else {
            RecruitApplies recruitApplies = appliesService.getRecruitApplies(id);
            //logger.info(recruitApplies.);
            RecruitInfo recruitInfo = noticeListService.getNoticeList(recruitApplies.getInfoid());
            String s = recruitInfo.getDeparmentid();

            Departmentname departmentname = departmentService.nametoDepartment(s);

            Departmentpeople departmentpeople = new Departmentpeople();
            departmentpeople.setId(recruitApplies.getId());
            departmentpeople.setDepartmentnameid(departmentname.getdepartmentid());
            //加入peopledepartment
            Integer aa = departmentPeopleService.createDepartmentPeople(departmentpeople);
            if (aa == 1) {
                IsSuccess = appliesService.deleteRecruitApplies(id);
            } else if (aa == 0) {
                logger.error("创建部门人员失败");
                 IsSuccess = 0;
            }
            //出现其他意外情况

        }
        JSONObject object=new JSONObject();
        if (IsSuccess==1){
            logger.info("删除成功");

            object.put("status","success");
        }else{
            logger.info("删除错误");
            object.put("status","false");
        }
        return object.toJSONString();
    }
}
